package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserJudges;
import com.ruoyi.system.domain.SysUsers;
import com.ruoyi.system.domain.SyseEventsPosted;
import com.ruoyi.system.domain.mongodb.Teacher;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mapper.SysEventsPostedMapper;
import com.ruoyi.system.mapper.SysUserJudgesMapper;
import com.ruoyi.system.mapper.SysUsersMapper;
import com.ruoyi.system.mongo.StudentDao;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.mongo.TeacherDao;
import com.ruoyi.system.service.SysUserJudgesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 柳
 * @Date 2021/3/26
 * @Description TODO
 */
@Service
@Transactional
public class SysUserJudgesServiceImpl implements SysUserJudgesService {

    @Autowired
    private SysUserJudgesMapper sysUserJudgesMapper;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private SysEventsPostedMapper sysEventsPostedMapper;

    @Autowired
    private SysUsersMapper sysUsersMapper;

    @Autowired
    private SysUserDao userDao;
    /**
     * 发送邀请生成一条教师邀请信息
     * @param sysUserJudges
     */
    @Override
    public void saveSysUserJudgesMapper(SysUserJudges sysUserJudges) {
        //根据教师id查询教师个人信息
        Users teacher = studentDao.selectUsersByUserId(sysUserJudges.getUid());
        sysUserJudges.setEmail(teacher.getEmail());
        sysUserJudges.setNickname(teacher.getNickname());
        sysUserJudges.setPhoneNumber(teacher.getPhoneNum());
        sysUserJudges.setRealname(teacher.getRealname());
        sysUserJudges.setNickType(1);
        sysUserJudgesMapper.saveSysUserJudgesMapper(sysUserJudges);
    }

    /**
     * 修改邀请状态为已接受
     * @param jid
     */
    @Override
    public void updateStateAccept(Integer jid) {
        sysUserJudgesMapper.updateStateAccept(jid);
    }


    /**
     * 修改邀请状态为已拒绝
     * @param jid
     */
    @Override
    public void updateStateRefuse(Integer jid) {
        sysUserJudgesMapper.updateStateRefuse(jid);
    }

    @Override
    public Integer insertSysUserJudges(String userIds, Integer epid, String epstage) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //1.根据赛事id查询赛事信息
        SyseEventsPosted eventsPosteed = sysEventsPostedMapper.selectById(epid);
        //2.根据赛事名称和赛事阶段  获取到具体的一个赛事
        Map<String, Object> param = new HashMap<>(2);
        param.put("eptitle",eventsPosteed.getEptitle());  //赛事标题
        param.put("epStage",epstage);  //赛事名称
        SyseEventsPosted syseEventsPosted = sysEventsPostedMapper.selectByEpIdAndEpStage(param);
        //3.根据用户的id查询到需要邀请的用户
        String[] userIdArrs = userIds.substring(0, userIds.length() - 1).split(",");
        Map<String, Object> paramLike = new HashMap<>();
        paramLike.put("userIds",userIdArrs);  //已经报名该赛事人的id
        List<SysUsers> sysUsersList = sysUsersMapper.selectUserCondition02(paramLike);
        //4.增加用户到 赛事邀请表中
        for(SysUsers sysUsers:sysUsersList){
            SysUserJudges sysUserJudges = new SysUserJudges();
            sysUserJudges.setUid(sysUsers.getUserId().toString());//用户编号
            sysUserJudges.setNickname(sysUsers.getNickName());//昵称
            sysUserJudges.setRealname(sysUsers.getTname());//名称
            sysUserJudges.setNickType(Integer.parseInt(sysUsers.getNickType()));//用户类型
            sysUserJudges.setEmail(sysUsers.getEmail());//邮箱
            sysUserJudges.setPhoneNumber(sysUsers.getPhoneNumber());
            sysUserJudges.setStatus(2); // 2邀请中
            sysUserJudges.setEpid(syseEventsPosted.getEpid());//赛事id
            sysUserJudges.setCreationTime(sdf.format(new Date()));
            sysUserJudgesMapper.saveSysUserJudgesMapper(sysUserJudges);
        }
        return 1;
    }

    @Override
    public Integer invokeTeacherToJudges(String teacherIds, Integer epid, String epstage) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //1.根据赛事id查询赛事信息
        SyseEventsPosted eventsPosteed = sysEventsPostedMapper.selectById(epid);
        //2.根据赛事名称和赛事阶段  获取到具体的一个赛事
        Map<String, Object> param = new HashMap<>(2);
        param.put("eptitle",eventsPosteed.getEptitle());  //赛事标题
        param.put("epStage",epstage);  //赛事名称
        SyseEventsPosted syseEventsPosted = sysEventsPostedMapper.selectByEpIdAndEpStage(param);

        //3.根据用户的id查询到需要邀请的用户
        String[] userIdArrs = teacherIds.split(",");
        for (String tid : userIdArrs) {
            if (StringUtils.isEmpty(tid)) {
                continue;
            }
            SysUsers sysUsers = sysUsersMapper.queryByTid(tid);
            System.err.println(sysUsers);
            SysUsers sysUser = new SysUsers();
            //如果mysql的用户表里没有当前老师的账号
            if (sysUsers == null) {
                Users user = userDao.queryUserById(tid);
                sysUser.setAvatar(user.getHeadimg());
                sysUser.setNickName(user.getNickname());
                sysUser.setUserName(user.getUsername());
                sysUser.setPhoneNumber(user.getPhoneNum());
                sysUser.setEmail(user.getEmail());
                sysUser.setTid(user.getId());
                sysUser.setNickType("01");
                sysUser.setStatus('0');
                sysUser.setDelFlag('0');
                sysUser.setTname(user.getRealname());
                sysUser.setTpwd(user.getPassword());//mongodb中的密码
                sysUser.setIsinvitation('2'); //2=邀请中，0=拒绝，1=接受
                sysUser.setIsmainjudges('0'); //是否为主评审员， 0=不是，1=是
                sysUser.setIsJudges('1'); //1=是评委，0=不是
                //将mongodb用户插入到mysql系统用户中
                sysUsersMapper.insertUser(sysUser);
                sysUsers = sysUser;
            }
            SysUserJudges sysUserJudges = new SysUserJudges();
            sysUserJudges.setUid(sysUsers.getTid());//用户编号
            sysUserJudges.setNickname(sysUsers.getNickName());//昵称
            sysUserJudges.setRealname(sysUsers.getTname());//名称
            sysUserJudges.setNickType(Integer.parseInt(sysUsers.getNickType()));//用户类型
            sysUserJudges.setEmail(sysUsers.getEmail());//邮箱
            sysUserJudges.setPhoneNumber(sysUsers.getPhoneNumber());
            sysUserJudges.setStatus(2); // 2邀请中
            sysUserJudges.setEpid(syseEventsPosted.getEpid());//赛事id
            sysUserJudges.setCreationTime(sdf.format(new Date()));
            sysUserJudgesMapper.saveSysUserJudgesMapper(sysUserJudges);
        }
        /*Map<String, Object> paramLike = new HashMap<>();
        paramLike.put("userIds",userIdArrs);  //已经报名该赛事人的id
        List<SysUsers> sysUsersList = sysUsersMapper.selectUserCondition02(paramLike);
        //4.增加用户到 赛事邀请表中
        for(SysUsers sysUsers:sysUsersList){
            SysUserJudges sysUserJudges = new SysUserJudges();
            sysUserJudges.setUid(sysUsers.getUserId().toString());//用户编号
            sysUserJudges.setNickname(sysUsers.getNickName());//昵称
            sysUserJudges.setRealname(sysUsers.getTname());//名称
            sysUserJudges.setNickType(Integer.parseInt(sysUsers.getNickType()));//用户类型
            sysUserJudges.setEmail(sysUsers.getEmail());//邮箱
            sysUserJudges.setPhoneNumber(sysUsers.getPhoneNumber());
            sysUserJudges.setStatus(2); // 2邀请中
            sysUserJudges.setEpid(syseEventsPosted.getEpid());//赛事id
            sysUserJudges.setCreationTime(sdf.format(new Date()));
            sysUserJudgesMapper.saveSysUserJudgesMapper(sysUserJudges);
        }*/
        return 1;
    }

    @Override
    public Integer deleteByJid(Integer jid) {
        return sysUserJudgesMapper.deleteByJid(jid);
    }
}
